home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 December / DPPCPRO1205.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.1.exe / $INSTDIR / Programs / Tube.gb < prev    next >
Encoding:
Text File  |  2005-07-29  |  5.1 KB  |  156 lines

  1. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  2. ' Tube
  3.  
  4. ' Settings
  5. const l = 20, w = 20            ' Tunnel length and width in segments
  6. const segl# = 10, segw# = 20    ' Segment size
  7. const cMax = 255, cMin = 1, cRnd = 15
  8.  
  9. ' Tube map
  10. ' 2D grid of colour components
  11. struc Cell
  12.     dim r, g, b, ang#
  13. endstruc
  14. dim Cell cells (w)(l)
  15.  
  16. ' Viewpoint & velocity
  17. dim vz#, vang#, voffset
  18. dim vzSpeed#, vangSpeed#
  19. vzSpeed# = 1.0: vangSpeed# = 0.3
  20.  
  21. ' Working
  22. dim x, y, i1, i2, i3, i4, t, counter#, scale#
  23. dim Cell &curCell, Cell &lCell, Cell &mCell, Cell &rCell
  24. dim Cell &tl, Cell &bl, Cell &tr, Cell &br
  25.  
  26. ' Textures
  27. dim tex
  28. tex = LoadMipmapTexture ("textures\00004.jpg")
  29.  
  30. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  31. ' Subroutines
  32. goto start
  33.  
  34. GenerateRow:
  35.     
  36.     ' Generate a row of colours based on the previous one
  37.     i1 = y * w
  38.     i2 = ((y + l - 1) % l) * w
  39.     for x = 0 to w - 1
  40.  
  41.         &curCell = &cells (x) (y)
  42.         &lCell = &cells ((x + w - 1) % w) ((y + l - 1) % l)
  43.         &mCell = &cells ((x + w    ) % w) ((y + l - 1) % l)
  44.         &rCell = &cells ((x +     1) % w) ((y + l - 1) % l)
  45.  
  46.         ' Average colours from previous row
  47.         curCell.r = (lCell.r + mCell.r*2 + rCell.r) / 4
  48.         curCell.g = (lCell.g + mCell.g*2 + rCell.g) / 4
  49.         curCell.b = (lCell.b + mCell.b*2 + rCell.b) / 4
  50.  
  51.         ' Add random offset
  52.         curCell.r = curCell.r + rnd()%(cRnd*2+1)-cRnd
  53.         curCell.g = curCell.g + rnd()%(cRnd*2+1)-cRnd
  54.         curCell.b = curCell.b + rnd()%(cRnd*2+1)-cRnd
  55.  
  56.         ' Clamp colour components
  57.         if curCell.r < cMin then curCell.r = cMin endif
  58.         if curCell.g < cMin then curCell.g = cMin endif
  59.         if curCell.b < cMin then curCell.b = cMin endif
  60.         if curCell.r > cMax then curCell.r = cMax endif
  61.         if curCell.g > cMax then curCell.g = cMax endif
  62.         if curCell.b > cMax then curCell.b = cMax endif
  63.  
  64.         ' Setup angle
  65.         curCell.ang# = 2 * m_pi / w * (x + (rnd()%100)/100.0)
  66.     next
  67.     return
  68.  
  69. start:
  70.     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  71.     ' Setup original map
  72.     ' Use random values for first row
  73.     y = 0: gosub generateRow
  74.     for x = 0 to w - 1
  75.         cells(x)(0).r = rnd()%100
  76.         cells(x)(0).g = rnd()%100
  77.         cells(x)(0).b = rnd()%100
  78.     next
  79.  
  80.     ' Generate remaining rows
  81.     for y = 1 to l - 1
  82.         gosub GenerateRow
  83.     next
  84.  
  85.     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  86.     ' Setup OpenGL
  87.     glDisable (GL_CULL_FACE)
  88.     glEnable (GL_TEXTURE_2D)
  89.     glBindTexture (GL_TEXTURE_2D, tex)
  90.     
  91.     TextMode (TEXT_OVERLAID)
  92.     dim a$: a$ = "Tube"
  93.     locate (TextCols () - len(a$)) / 2, TextRows() / 2
  94.     print a$
  95.     
  96.     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  97.     ' Main loop
  98.  
  99. '    scale# = segw# / cMax * 0.8
  100.     while true
  101.         
  102.         ' Render tunnel
  103.         glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
  104.         glMatrixMode (GL_MODELVIEW)
  105.         glLoadIdentity ()
  106.         glTranslatef (0, 0, vz#)
  107.         glRotatef (vang#, 0, 0, 1)
  108.  
  109.         for t = 0 to l - 2
  110.             y = (t + voffset) % l
  111.             for x = 0 to w
  112.                 
  113.                 ' Find vertex indices
  114.                 &tl = &cells (x)((y+1)%l)
  115.                 &tr = &cells ((x+1)%w)((y+1)%l)
  116.                 &bl = &cells (x)(y)
  117.                 &br = &cells ((x+1)%w)(y)
  118.                 
  119.                 ' Render slice of tube
  120.                 glBegin (GL_QUADS)
  121.                     glTexCoord2f (0, 0): glColor3ub (tl.r, tl.g, tl.b): glVertex3f (sin(tl.ang#) * segw#, cos(tl.ang#) * segw#, -segl#)
  122.                     glTexCoord2f (1, 0): glColor3ub (tr.r, tr.g, tr.b): glVertex3f (sin(tr.ang#) * segw#, cos(tr.ang#) * segw#, -segl#)
  123.                     glTexCoord2f (1, 1): glColor3ub (br.r, br.g, br.b): glVertex3f (sin(br.ang#) * segw#, cos(br.ang#) * segw#, 0)
  124.                     glTexCoord2f (0, 1): glColor3ub (bl.r, bl.g, bl.b): glVertex3f (sin(bl.ang#) * segw#, cos(bl.ang#) * segw#, 0)
  125.                 glEnd ()
  126.             next
  127.  
  128.             glTranslatef (0, 0, -segl#)
  129.         next
  130.                 
  131.         DrawText ()
  132.         SwapBuffers ()        
  133.  
  134.         ' Update camera position
  135.         while SyncTimer (10)
  136.  
  137.             ' Respond to keyboard
  138.             if ScanKeyDown (VK_LEFT)  then vangSpeed# = vangSpeed# - 0.01 endif
  139.             if ScanKeyDown (VK_RIGHT) then vangSpeed# = vangSpeed# + 0.01 endif
  140.             if ScanKeyDown (VK_UP)    then vzSpeed# = vzSpeed# + 0.01 endif
  141.             if ScanKeyDown (VK_DOWN)  then vzSpeed# = vzSpeed# - 0.01 endif
  142.             if vzSpeed# < 0 then vzSpeed# = 0 endif
  143.  
  144.             ' Move camera forwards
  145.             vz# = vz# + vzSpeed#
  146.             while vz# > segl#
  147.                 vz# = vz# - segl#
  148.                 y = voffset % l
  149.                 gosub GenerateRow
  150.                 voffset = voffset + 1
  151.             wend
  152.  
  153.             ' Rotate camera
  154.             vang# = vang# + vangSpeed#
  155.         wend                
  156.     wend